

package com.trade.dao.impl;

import java.util.List;

import com.trade.common.dao.impl.GenericDao;
import com.trade.dao.ICustomerDao;
import com.trade.model.Customer;

/**
 * 
 * @author dean.peng
 * 
 */
public class CustomerDaoImpl extends GenericDao<Customer, Long> implements ICustomerDao {

    @Override
    public Customer getByCustomerName(String strCustomer) {
        List<Customer> users = find("from Customer where CustomerName=?", strCustomer);
        if (users == null || users.size() == 0) {
            return null;
        }
        return users.get(0);
    }
    
    @Override
    public Boolean AddCustomer(Customer customer)
    {
    	try {
    		List<Customer> customers = find("from Customer where CustomerName=?", customer.getCustomerName());
    		if(customers!=null&&customers.size()>0)
    		{
    			return false;
    		}
    		else
    		{
    			save(customer);
    		}
    	} catch(Exception ex) {
    		return false;	
    	}
        return true;
    }
    
    @Override
    public Boolean UpdateCustomer(Customer customer)
    {
    	try {
    		update(customer);
    	} catch(Exception ex) {
    		return false;	
    	}
        return true;
    }
    
    @Override
    public List<Customer> getAllCustomers()
    {
    	List<Customer> customers = find("from Customer",null);
        if (customers == null || customers.size() == 0) {
            return null;
        }
        return customers;
    }
    

    @Override
    public Boolean DeleteCustomer(String strCustomerID)
    {
    	try {
    		List<Customer> customers = find("from Customer where CustomerID=?", strCustomerID);
    		delete(customers.get(0));
    	} catch(Exception ex) {
    		return false;	
    	}
        return true;
    }
    
    @Override
   public Customer GetCustomerById(String strCustomerID)
    {
         List<Customer> users = find("from Customer where CustomerID=?", strCustomerID);
         if (users == null || users.size() == 0) {
             return null;
         }
         return users.get(0);
    }

}
